package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcAppConfigAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcDeleteListRequest;
import org.tis.tools.abf.module.ac.entity.AcAppConfig;
import org.tis.tools.abf.module.ac.service.IAcAppConfigService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acAppConfig的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acAppConfig")
@Validated
@Slf4j
public class AcAppConfigController extends BaseController<AcAppConfig> {

    @Autowired
    private IAcAppConfigService acAppConfigService;

    @ApiOperation(value = "新增应用的个性化配置", notes = "传入AcAppConfigAddRequest类为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增个性化配置")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(name = "AcAppConfigAddRequest",value = "新增时需要传入的参数",
            required = true) AcAppConfigAddRequest acAppConfigAddRequest) {

        acAppConfigService.createRootAppConfig(acAppConfigAddRequest.getGuidApp(), acAppConfigAddRequest.getConfigType(),acAppConfigAddRequest.getConfigName(), acAppConfigAddRequest.getConfigDict(), acAppConfigAddRequest.getConfigStyle(), acAppConfigAddRequest.getConfigValue(),acAppConfigAddRequest.getEnabled(), acAppConfigAddRequest.getDisplayOrder(),acAppConfigAddRequest.getConfigDesc());
        return ResultVO.success("新增成功！");
    }

    @ApiOperation(value = "修改应用的个性化配置",notes = "传入AcAppConfigAddRequest类为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改个性化配置")
    @PutMapping
    public ResultVO update(@RequestBody @Validated AcAppConfigAddRequest acAppConfigAddRequest) {

       AcAppConfig acAppConfig = acAppConfigService.selectById(acAppConfigAddRequest.getGuid());
        if (acAppConfig == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        AcAppConfig acAppConfig1 = acAppConfigService.changeById(acAppConfigAddRequest.getGuid(),acAppConfigAddRequest.getGuidApp(),
                acAppConfigAddRequest.getConfigType(), acAppConfigAddRequest.getConfigName(), acAppConfigAddRequest.getConfigDict(),
                acAppConfigAddRequest.getConfigStyle(), acAppConfigAddRequest.getConfigValue(), acAppConfigAddRequest.getEnabled(),
                acAppConfigAddRequest.getDisplayOrder(),acAppConfigAddRequest.getConfigDesc());
        return ResultVO.success("修改成功！",acAppConfig1);
    }

    @ApiOperation(value = "根据ID删除应用的个性化配置",notes = "传入id为参数")
    @OperateLog(type = OperateType.DELETE, desc = "删除个性化配置")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value = "个性化配置的guid",
            required = true) String id) {
        AcAppConfig acAppConfig = acAppConfigService.selectById(id);
        if (acAppConfig == null ) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acAppConfigService.moveAppConfig(id);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "根据ID查询应用的个性化配置",notes = "传入id为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "id",value = "个性化配置的guid",
            required = true) String id) {
        AcAppConfig acAppConfig = acAppConfigService.selectById(id);
        if (acAppConfig == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acAppConfig);
    }

    @ApiOperation(value = "分页查询应用个性化配置值",notes = "传入分页的参数(page)和筛选条件(condition)")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(name = "page",value = "分页查询的分页条件",required = true)
                                     SmartPage<AcAppConfig> page) {
        return ResultVO.success("查询成功", acAppConfigService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }

    @ApiOperation(value = "根据应用ID分页查询对应应用个性化配置值",notes = "传入分页的参数(page)和筛选条件(condition),应用的guid")
    @PostMapping("/listByAppId/{appId}")
    public ResultVO listByAppId(@RequestBody @Validated @ApiParam(name = "page",value = "分页查询的分页条件",required = true) SmartPage<AcAppConfig> page,@PathVariable @NotBlank(message = "id不能为空") @ApiParam(name = "appId",value = "应用的guid", required = true) String
            appId){
        return ResultVO.success("查询成功",acAppConfigService.listByAppId(getPage(page,AcAppConfig.class),page.getConditionWrapper(), appId));
    }

    @ApiOperation(value = "批量删除",notes = "传入需要删除的id数组(deleteList)")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(name = "deleteList",value = "批量删除的条件",required = true)
                                AcDeleteListRequest deleteListRequest){
            acAppConfigService.batchDelete(deleteListRequest);
        return ResultVO.success("删除成功");
    }

}

